﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase;
using DtuService;

namespace DtuService
{
    class Log2Net : Client
    {
        private const string pleaseInputUserName = "输入用户名：";
        private const string pleaseInputPassword = "请输入密码：";
        private const string checkOK = "\r\n登录成功！\r\n";
        private const string errorCheck = "\r\n验证失败！\r\n";
        private int inputStep = 1;
        private string userName = "";
        private string password = "";
        private bool checkPass = false;
        public Log2Net(ByteSession tcp) : base(tcp)
        {
            byte[] userName = Encoding.GetEncoding("gb2312").GetBytes(pleaseInputUserName);
            bSession.Send(userName, 0, userName.Length);
            bSession.AppServer.NewRequestReceived += AppServer_NewRequestReceived;
        }

        private void AppServer_NewRequestReceived(ByteSession session, SuperSocket.SocketBase.Protocol.BinaryRequestInfo requestInfo)
        {
            if (session.ClientInstance == this)
            {
                if (requestInfo.Key != "13")
                {
                    if (inputStep == 1)
                    {
                        userName += Encoding.GetEncoding("gb2312").GetString(requestInfo.Body);
                    }
                    else if(inputStep == 2)
                    {
                        password += Encoding.GetEncoding("gb2312").GetString(requestInfo.Body);
                    }
                }
                else
                {
                    if (inputStep == 1)
                    {
                        byte[] userName = Encoding.GetEncoding("gb2312").GetBytes(pleaseInputPassword);
                        base.bSession.Send(userName, 0, userName.Length);
                        inputStep = 2;
                    }
                    else if (inputStep == 2)
                    {
                        try
                        {
                            Busness.Business.getInstance().checkPwd(userName, password, 
                                new DataAccess.ExcuteSqlEventHander((object sender, DataAccess.ExcuteSqlEventArgs e) =>
                            {
                                if ((int)e._returnValue >= 1)
                                {
                                    checkPass = true;
                                    Debug.Log("Telnet 连接 用户名 ： " + userName);
                                   // WriteLog.OutPutLog += WriteLog_OutPutLog;
                                    byte[] ok = Encoding.GetEncoding("gb2312").GetBytes(checkOK);
                                    base.bSession.Send(ok, 0, ok.Length);
                                }
                                else
                                {
                                    byte[] error = Encoding.GetEncoding("gb2312").GetBytes(errorCheck);
                                    base.bSession.Send(error, 0, error.Length);
                                    base.bSession.Close();
                                }
                            }));
                        }
                        catch
                        {
                            byte[] error = Encoding.GetEncoding("gb2312").GetBytes(errorCheck);
                            base.bSession.Send(error, 0, error.Length);
                            base.bSession.Close();
                        }
                        inputStep = 3;
                    }
                }
            }
        }

        private void WriteLog_OutPutLog(string log)
        {
            SendLog2TelNet(log);
        }
        
        public void SendLog2TelNet(string log)
        {
            byte[] byteLog = Encoding.GetEncoding("gb2312").GetBytes(log);
            bSession.Send(byteLog,0, byteLog.Length);
        }

        public override void OnShutdown()
        {
            if (checkPass)
            {
                //WriteLog.OutPutLog -= WriteLog_OutPutLog;
            }
            Debug.Log("Telnet 日志输出断开 用户名 ：" + userName);
        }
    }
}
